<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 操作日志控制器
 * @package app\index\controller
 */
class EquipmentLog extends Auth
{
    public $noNeedLogin = [];

    /**
     *操作日志列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            $sql = Db::name('qt_equipment_log')
                ->alias('l')
                ->leftJoin('qt_room r','r.id = l.room_id')
                ->leftJoin('qt_users u','u.id = l.engineer_id')
                ->leftJoin('qt_user_menu m','m.id = l.operation_type')
                ->field('l.id,l.operation_type,l.equipment,l.content,r.name as room_name,u.work_number,l.create_time,l.user_id')
                ->where(['l.store_id' => $userInfo['store_id']]);
            //房间筛选
            if(isset($getData['room_name']) && !empty($getData['room_name'])){
                $sql = $sql->whereLike('r.name','%'.$getData['room_name'].'%');
            }
            //技师号
            if(isset($getData['work_number']) && !empty($getData['work_number'])){
                $sql = $sql->where('u.work_number',$getData['work_number']);
            }
            //时间筛选
            if(isset($getData['start_time']) && !empty($getData['start_time'])){
                $start_time = strtotime($getData['start_time']);
                $end_time = strtotime($getData['end_time']);
                $sql = $sql->whereBetweenTime('l.create_time',$start_time,$end_time);
            }
            //操作用户
            if(isset($getData['user_id']) && !empty($getData['user_id'])){
                $sql = $sql->where('u.id',$getData['user_id']);
            }
            //操作类型
            if(isset($getData['operation_type']) && !empty($getData['operation_type'])){
                $sql = $sql->where('l.operation_type',$getData['operation_type']);
            }
            //操作设备
            if(isset($getData['equipment']) && !empty($getData['equipment'])){
                $sql = $sql->where('l.equipment',$getData['equipment']);
            }
            $data = $sql->order('l.id desc')->paginate($getData['limit'])->each(function ($item){
                $item['user_name'] = Db::name('qt_users')->where('id',$item['user_id'])->value('real_name');
                return $item;
            });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加房间
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['mac']) || empty($getData['room_id'])){
                $this->result('缺少参数', '', 204);
            }
            $insert = Db::name('qt_room_bell')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑保存房间
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['mac']) || empty($getData['room_id']) || empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $update= Db::name('qt_room_bell')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑保存房间
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_room_bell')->where('id',$getData['id'])->find();
            $this->result('获取成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除房间
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
             Db::name('qt_room_bell')->where('id',$getData['id'])->delete();
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }



}
